package com.dd.utils.file

import android.content.Context
import android.util.Log
import java.io.File

const val PATH_DZJ = "/dd"

object FileUtil {

    fun createFolder(context: Context) {
        getSandBoxDir(PATH_DZJ, context)?.let {
            val file: File = it
            if (!file.exists()) {
                val mkdirs = file.mkdirs()
                if (!mkdirs) {
                    Log.e("FileUtil", "=====创建文件夹失败=====")
                } else {
                    Log.e("FileUtil", "=====创建文件夹成功=====")
                }
            } else {
                Log.e("FileUtil", "=====创建文件已经存在=====")
            }
        }
    }

    /**
     * sd卡上创建文件和得到文件
     *
     * @param path
     * @param fileName
     * @return 创建成功的文件
     * @throws Exception
     */
    fun newFile(path: String = PATH_DZJ, fileName: String, context: Context): File {
        val file:File? = getSandBoxDir(path, context)?.let {
            val pathFile: File = it
            if (!pathFile.exists()) {
                val mkdirs = pathFile.mkdirs()
                if (!mkdirs) {
                    Log.e("FileUtil", "=====创建文件夹失败=====")
                }
            }
            pathFile
        }
        return File(file, fileName)
    }

    fun getSandBoxDir(dirName: String, context: Context): File? {
        return context.getExternalFilesDir(dirName)
    }
}